﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime;

namespace PSLib.Parsing.Common.Search
{
    /// <summary>
    /// Compares current token position with SnapshotPoint position
    /// </summary>
    public class ElementPositionComparer : IComparer<object>
    {
        public ElementPositionComparer(CommonTokenStream stream)
        {
            this.CommonTokenStream = stream;
        }
        public int Compare(object x, object y)
        {
            ElementPosition point = (ElementPosition)y;
            IToken token = (IToken)x;
            ElementSpan tokenSpan = token.GetSpan(this.CommonTokenStream);
            if (tokenSpan.End == ElementPosition.GlobalEnd) return -1;
            int result = tokenSpan.Compare(point);
            return result;
        }
        
        private CommonTokenStream CommonTokenStream { get; set; }
    }
}
